<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Gt extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		$this->load->library('get_data_html','html');
		$this->load->model('category_term_model','mcategory');
		$this->load->model('post_m');
		$this->load->model('term_posts_m', 'term_posts');
	}
	
	function index()
	{
		
	}
	
	function detail()
	{
		$cats = $this->mcategory->get_many_by('term_parent >', 0);
		foreach($cats as $cat)
		{
			$cat_url = $cat->get_url;
			$cat_id = $cat->term_id;
			for($i =10; $i>0; $i--)
			{
				$link = $cat_url.'?trang='.$i;
				$html = $this->get_data_html->get_data($link);
				$html = $this->get_data_html->GetBetween($html, '<ul class="list-articles">', '</ul>');
				$links = $this->get_data_html->load($html, array(
				'estart' =>'<li>',
				'eend' =>'<\/li>',
				));
				for($i = count($links) -1; $i >=0; $i--)
				{
					$arr_insert = array();
					
					$html = $links[$i];
					$thumb = $this->get_data_html->GetBetween($html, 'src="', '"');
					$title = $this->get_data_html->GetBetween($html, '<h2 class="p-title">', '</h2>');
					$post_excerpt = $this->get_data_html->GetBetween($html, '<div class="p-excerpt">', '</div>');
					$time = $this->get_data_html->GetBetween($html, '<p class="p-time">', '</p>');
					$link = $this->get_data_html->GetBetween($html, 'href="', '"');
					
					$check = $this->post_m->get_by('get_url', $link);
					
					if($check) 
					{
						$this->term_posts->insert(array(
							'post_id' =>$check->post_id,
							'term_id' =>$cat_id,
						));
						continue;
					}
					
					$title = strip_tags($title);
					$post_excerpt = strip_tags($post_excerpt);
					$time = strip_tags($time);
					$time = explode('|',$time);
					$time = @$time[1];
					$time = strtotime($time);
					
					$arr_insert['post_thumb'] = $thumb;
					$arr_insert['cat_id'] = $cat_id;
					$arr_insert['admin_id'] = 1;
					$arr_insert['post_title'] =$title;
					$arr_insert['post_excerpt'] =$post_excerpt;
					$arr_insert['post_time_start'] =$time;
					$arr_insert['get_url'] =$link;
					$arr_insert['post_type'] = 1;
					$arr_insert['post_status'] = 1;
					
					$html_content = $this->get_data_html->get_data($link);
					$html_content = $this->get_data_html->GetBetween($html_content, '<div id="bodycontent">', '<div class="clearfix">');
					$arr_insert['post_content'] = $html_content;
					
					$new_id = $this->post_m->insert($arr_insert, TRUE);
					$this->term_posts->insert(array(
					'post_id' =>$new_id,
					'term_id' =>$cat_id,
					));
					echo "\r".$new_id;
				}
			}
		}
	}
	
	function category()
	{
		$cats = array();
		
		$cats[] = array('name'=>'Khoa học - Giáo dục', 'parent'=>0, 'url' =>'http://motthegioi.vn/khoa-hoc-giao-duc/');
		$cats[] = array('name'=>'Quốc tế', 'parent'=>0, 'url' =>'http://motthegioi.vn/quoc-te/');
		$cats[] = array('name'=>'Xã hội', 'parent'=>0, 'url' =>'http://motthegioi.vn/xa-hoi/');
		$cats[] = array('name'=>'Kinh tế', 'parent'=>0, 'url' =>'http://motthegioi.vn/kinh-te/');
		$cats[] = array('name'=>'Văn hóa - Giải trí', 'parent'=>0, 'url' =>'http://motthegioi.vn/van-hoa-giai-tri/');
		$cats[] = array('name'=>'Thế giới mạng', 'parent'=>0, 'url' =>'http://motthegioi.vn/the-gioi-mang/');
		$cats[] = array('name'=>'Luxury Living', 'parent'=>0, 'url' =>'http://motthegioi.vn/luxury-living/');
		$cats[] = array('name'=>'Thể thao', 'parent'=>0, 'url' =>'http://motthegioi.vn/thethao/');
		$cats[] = array('name'=>'Lý dịch', 'parent'=>0, 'url' =>'http://motthegioi.vn/ly-dich/');
		$cats[] = array('name'=>'Cầu vồng lục sắc', 'parent'=>0, 'url' =>'http://motthegioi.vn/cau-vong-luc-sac/');
		$cats[] = array('name'=>'Thế giới xe', 'parent'=>0, 'url' =>'http://motthegioi.vn/the-gioi-xe/');
		$cats[] = array('name'=>'Sức khỏe', 'parent'=>0, 'url' =>'http://motthegioi.vn/suckhoe/');
		
		foreach($cats as $cat)
		{
			//sub
			$link = $cat['url'];
			$cat_id = $this->mcategory->insert(
			array(
			'term_name' =>$cat['name'],
			'term_slug' =>str_replace('--','-', create_slug($cat['name'])),
			'term_type' => 'category',
			'get_url' => $link,
			)
			);
			echo $cat['name']."<br>";
			$html = $this->get_data_html->get_data($link);
			$html = $this->get_data_html->GetBetween($html, '<div class="second-nav">', '</div>');
			$links = $this->get_data_html->load($html, array(
			'estart' =>'<li>',
			'eend' =>'<\/li>',
			));
			if($links)
			{
				foreach($links as $link)
				{
					$url = $this->get_data_html->GetBetween($link, 'href="', '"');
					$title = strip_tags($link);
					$this->mcategory->insert(
					array(
					'term_name' =>$title,
					'term_slug' => str_replace('--','-', create_slug($title)),
					'term_type' => 'category',
					'term_parent' => $cat_id,
					'get_url' => $url,
					)
					);
					echo $title."<br>";
				}
			}
			echo "-------------<br>";
		}
		$this->mcategory->build_link_cache();
	}
	
	function t()
	{
		set_time_limit(999999999999);
		$file_data = $this->get_data_html->get_data(base_url().'data.html');
		$arr_data= explode("\n",$file_data);
		$this->load->helper('file');
		$k = '';
		foreach($arr_data as $i=>$link)
		{
			$link = 'http://'.$link;
			$url = 'http://seomxh.com/tools/moz/index.php';
			$form_data = array('url' =>$link,
			'seomxhmoz'=>''
			);
			$data = $this->get_data_html->get_data_post($url,$form_data,'http://seomxh.com/tools/moz/index.php');
			
			
			$pa = $this->get_data_html->GetBetween($data, '(PA)', '</p>');
			$pa = strip_tags($pa);
			$pa = str_replace(array('.',':'),'', $pa);
			$pa = trim($pa);
			$pa = trim($pa);
			
			$da = $this->get_data_html->GetBetween($data, '(DA)', '</p>');
			$da = strip_tags($da);
			$da = str_replace(array('.',':'),'', $da);
			$da = trim($da);
			
			$el = $this->get_data_html->GetBetween($data, '(EL)', '</p>');
			$el = strip_tags($el);
			$el = str_replace(array('.',':'),'', $el);
			$el = trim($el);
			/*
			$pr_data = $this->get_data_html->get_data('http://www.prapi.net/pr.php?url='.$link.'&f=json');
			$pr_data = json_decode($pr_data);
			$pr = $pr_data->pagerank;
			*/
			
			$k= $da."\t".$pa."\t".$el."\n";
			echo $k;
			// usleep(500);
			 write_file('./ht.txt', $k,'a+');
			
		}
			
		// echo $pa;
	}
	 
}